﻿@{
    ViewBag.Title = "AtutoMarketing";
}
@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script>
}
@{
    <style type="text/css">
        div {
            overflow:hidden;
        }
    </style>
}
<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
    <table>
        <tr>
            <td width="200px;">
                <a class="mini-button" id="SaveInfo" name="SaveInfo" iconCls="icon-save" onclick="Save" plain="true">保存</a>
                <span class="separator"></span>
                <a class="mini-button" iconcls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
            </td>
        </tr>
    </table>       
</div>
     <form id="form1" method="post"  style="height: 100%">
       <table style="margin-left:10px;padding-top:5px;">
            <tr>
                <td colspan="2">
                    <div id="ckuse" name="ckuse" checked="false" class="mini-checkbox" readOnly="false" text="启用"
                         TabIndex="1" style="margin-left:90px;float:left;"></div>
                    <div id="ckSMS" name="ckSMS" checked="false" class="mini-checkbox" readOnly="false" text="发送短信" 
                         TabIndex="2" style="margin-left:50px;float:left;"></div>
                    <div style="padding-left:8px;margin-top:3px;">(需要有充足短信余额)</div>
               </td>
            </tr>
           <tr>
                <td width="80px" height ="25px" align="right">
                    方案类型：</td>
                <td width="450px" height ="25px">
                    <input id="FType" name="FType" class="mini-combobox" style="width:450px;" showNullItem="false" 
                            allowInput="false" align="left" value ="1" data ="comType" tabindex="3" onvaluechanged="ChoiceType"/> 
                </td>
           </tr>
           <tr>
                <td width="80px" height ="25px" align="right">
                    方案名称：</td>
                <td width="450px" height ="25px">
                        <input id="FName" name="FName" class="mini-textbox " style="width:450px;"  TabIndex="4" selectOnFocus="true"/>
                </td>
           </tr>
            <tr>
                <td width="80px" height ="50px" align="right">
                    短信模板：</td>
                <td width="450px" height ="50px">
                    <textarea type="text" id="Template" name="Template" class="mini-textarea" 
                        vtype="rangeChar:1,2048" width="450px" height="130px" TabIndex="5"></textarea>
                    <div>{0}:会员编号, {1}:会员名称, {2}:会员电话号码, {3}:优惠券码<br />(注意: 70字符一条短信,超出则多算一条)</div>
                </td>
            </tr>
            <tr>
                <td width="80px" height ="25px" align="right">
                    礼券类型：</td>
                <td>
                    <table>
                        <tr>
                            <td width="110px">
                                <input id="FCouponNumber" name="FCouponNumber" class="mini-buttonedit" style="width: 110px;"
                                    onbuttonclick="getCouponItem('',1)" onvaluechanged="getCouponValueChanged0"  TabIndex="6" selectonfocus="true" />
                            </td>
                            <td width="180px">
                                <input id="FCouponName" name="FCouponName" class="mini-textbox" style="width: 180px; background-color: #CCCCCC;"
                                    enabled="false" align="left" readonly="readonly" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
                <tr>
                    <td width="80px" height ="25px" align="right">
                        <span id="DiffDaysTitle">生日距离(天)：</span></td>
                    <td width="450px" height ="25px">
                        <div style="float:left">
                        <input id="DiffDays" name="DiffDays" class="mini-spinner" emptyText="" minValue="0" maxValue="100" changeOnMousewheel ="false" 
                            style="width:120px;"  value="0" increment ="1"  TabIndex="7" selectOnFocus="true"/></div>
                        <div style="padding-left:10px;" id="Diffmark"><span id="DiffDaysMark">(0--100) 0为生日当天,1为提前1天,以此类推</span></div>
                    </td>
                </tr>
            <tr>
                <td colspan="2">
                    <table>
                        <tr>
                            <td width="80px" height ="25px" align="right">
                                开始日期：</td>
                            <td width="120px" height ="25px">
                                <input class="mini-datepicker" name="FStartDate" format="yyyy-MM-dd" style="width:120px;" value = ""  TabIndex="8"/>
                            </td>
                            <td width="100px" height ="25px" align="right">
                                结束日期：</td>
                            <td width="120px" height ="25px">
                                <input class="mini-datepicker" name="FEndDate" format="yyyy-MM-dd" style="width:120px;" value = ""  TabIndex="9"/>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td width="80px" height="25px" align="right">每天发送时间：</td>
                <td width="450px" height="25px">
                    <div style="float:left">
                        <input id="SendTime" name="SendTime" class="mini-spinner" emptyText="请输入短信发送时间(0-23)" minValue="0" maxValue="23" changeOnMousewheel ="false" 
                            style="width:120px;"  showbutton="true"  value="10" increment ="1" selectOnFocus="true"  TabIndex="10"/>
                    </div>
                    <div style="padding-left:10px;">(0--23)</div>
                </td>
            </tr>
            <tr>
                <td width="80px" height ="25px" align="right">
                    星期选择：</td>
                <td width="450px" height ="25px">
                    <div id="week1" name="week1"  class="mini-checkbox" readOnly="false" text="星期一" style="margin-right:7px;" TabIndex="11"></div>
                    <div id="week2" name="week2"  class="mini-checkbox" readOnly="false" text="星期二" style="margin-right:7px;" TabIndex="12"></div>
                    <div id="week3" name="week3"  class="mini-checkbox" readOnly="false" text="星期三" style="margin-right:7px;" TabIndex="13"></div>
                    <div id="week4" name="week4"  class="mini-checkbox" readOnly="false" text="星期四" style="margin-right:7px;" TabIndex="14"></div>
                    <div id="week5" name="week5"  class="mini-checkbox" readOnly="false" text="星期五" style="margin-right:7px;" TabIndex="15"></div>
                    <div id="week6" name="week6"  class="mini-checkbox" readOnly="false" text="星期六" style="margin-right:7px;" TabIndex="16"></div>
                    <div id="week0" name="week0"  class="mini-checkbox" readOnly="false" text="星期日" TabIndex="17"></div>
                </td>
            </tr>
        </table>
        <input name="FItemID" class="mini-hidden" />
        </form>
<script>
    var comType = [{ "id": "1", "text": "会员生日方案" },
                   { "id": "2", "text": "非活跃会员方案" }]

    mini.parse();

    $(function () {
        NewInfo();
        mini.getbyName("Template").setValue('尊敬的{1}，送您一张优惠券, 三天内有效, 祝您生日快乐！');
    });

    function SetData(data) {
        mini.getbyName("FItemID").setValue(data.FItemID);
        if(data.FItemID>0)
            LoadData(data.FItemID);
   }

    function LoadData(FItemID) {
        var json = '{ "mSystemAppID" : "' + parent.window.getSystemAppID() + '",';
        json += '"FItemID" : '+ FItemID +'}';
        $.post("GetMarketing", { datajson: json }, function (data) {
            if (data == '') return;
            var ret = mini.decode(data);
            if (ret.data.length == 0) return;
            setForm(ret.data[0]);
        });
    }

    function setForm(data) {
        if(data.FState==1)
            mini.getbyName("ckuse").setChecked(true);
        else
            mini.getbyName("ckuse").setChecked(false);
        if (data.FSendSMS == 1)
            mini.getbyName("ckSMS").setChecked(true);
        else
            mini.getbyName("ckSMS").setChecked(false);

        mini.getbyName("FItemID").setValue(data.FItemID);
        mini.getbyName("FType").setValue(data.FType);
        mini.getbyName("FName").setValue(data.FName);
        mini.getbyName("Template").setValue(data.FTemplate);
        mini.getbyName("DiffDays").setValue(data.FInactiveDays);
        mini.getbyName("FStartDate").setValue(data.FStartDate);
        mini.getbyName("FEndDate").setValue(data.FEndDate);
        mini.getbyName("SendTime").setValue(data.FStartTime);
        if (data.FMonday == 1)
            mini.getbyName("week1").setChecked(true);
        else
            mini.getbyName("week1").setChecked(false);
        if (data.FTuesday == 1)
            mini.getbyName("week2").setChecked(true);
        else
            mini.getbyName("week2").setChecked(false);
        if (data.FWednesday == 1)
            mini.getbyName("week3").setChecked(true);
        else
            mini.getbyName("week3").setChecked(false);
        if (data.FThursday == 1)
            mini.getbyName("week4").setChecked(true);
        else
            mini.getbyName("week4").setChecked(false);
        if (data.FFriday == 1)
            mini.getbyName("week5").setChecked(true);
        else
            mini.getbyName("week5").setChecked(false);
        if (data.FSaturday == 1)
            mini.getbyName("week6").setChecked(true);
        else
            mini.getbyName("week6").setChecked(false);
        if (data.FSunday == 1)
            mini.getbyName("week0").setChecked(true);
        else
            mini.getbyName("week0").setChecked(false);
        mini.getbyName("FCouponNumber").setValue(data.FCouponID);
        mini.getbyName("FCouponNumber").setText(data.FCouponNumber);
        mini.getbyName("FCouponName").setValue(data.FCouponName);
    }

    function NewInfo() {
        var nowDate = new Date();
        mini.getbyName("FStartDate").setValue(nowDate);
        mini.getbyName("FStartDate").setValue(nowDate);
        nowDate.setDate(nowDate.getDate() + 1);
        mini.getbyName("FEndDate").setValue(nowDate);
        mini.getbyName("FEndDate").setValue(nowDate);

        mini.getbyName("week0").setChecked(true);
        mini.getbyName("week1").setChecked(true);
        mini.getbyName("week2").setChecked(true);
        mini.getbyName("week3").setChecked(true);
        mini.getbyName("week4").setChecked(true);
        mini.getbyName("week5").setChecked(true);
        mini.getbyName("week6").setChecked(true);
    }

    function Save() {
        mini.getbyName("SaveInfo").setEnabled(false);
        if (mini.getbyName("FName").getValue().length > 200) {
            mini.alert("方案名称必须少于200个字符");
            return;
        }
        if (mini.getbyName("Template").getValue().length > 500) {
            mini.alert("方案短信模板必须少于500个字符");
            return;
        }
        var form0 = new mini.Form("#form1");
        var data0 = form0.getData(true, false);
        data0.FEndDate = mini.getbyName("FEndDate").getFormValue();
        var Data = {};
        Data.data = data0;
        Data.mSystemAppID = parent.window.getSystemAppID();
        var jsonstr = JSON.stringify(Data);

        $.post("SaveMarketing", { json: jsonstr }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").setEnabled(true);
        })
    }

    function getCouponValueChanged() {
        var sSearchKey = mini.get("FCouponNumber").getText();
        var mAppID = parent.window.getSystemAppID();
        if (sSearchKey == "") return;
        var sTbName = "Coupon";
        var json = "[{";
        json = json + "'FSystemAppID':" + "'" + mAppID + "',";
        json = json + "'FTbName':" + "'" + sTbName + "',";
        json = json + "'FClassID':" + "'0',";
        json = json + "'FNumber':" + "'" + sSearchKey + "'";
        json = json + "}]";
        $.post("../GetItem/GetItemList", { datajson: json }, function (text) {
            var sdata = mini.decode(text);
            if (sdata.data.length == 1) {
                    mini.get("FCouponNumber").setValue(sdata.data[0].FItemID);
                    mini.get("FCouponNumber").setText(sdata.data[0].FNumber);
                    mini.get("FCouponName").setValue(sdata.data[0].FName);
            }
            else {
                    mini.get("FCouponNumber").setValue("");
                    mini.get("FCouponNumber").setText("");
                    mini.get("FCouponName").setValue("");
            }
        });
    }

    function getCouponItem(mSearchKey) {
        mini.open({
            url: "../GetItem/Index",
            title: "礼券类型选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "Coupon", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
            },
            ondestroy: function (action) {
                if (action.length > 0) {
                        mini.getbyName("FCouponNumber").setValue(action[0].FItemID);
                        mini.getbyName("FCouponNumber").setText(action[0].FNumber);
                        mini.getbyName("FCouponName").setValue(action[0].FName);
                }
            }
        });
    }

    function ChoiceType() {
        if (mini.getbyName("FType").getValue() == 1) {
            $("#DiffDaysTitle").html("生日距离(天)：");
            $("#DiffDaysMark").html("(0--100) 0为生日当天,1为提前1天,以此类推");
        }
        else {
            $("#DiffDaysTitle").html("会员沉睡天数：");
            $("#DiffDaysMark").html("会员到现在连续未消费的天数");
        }
    }
</script>